package com.android.supermarket.activity

import android.os.Bundle
import android.view.Menu
import android.view.View
import android.widget.Button
import android.widget.EditText
import com.android.supermarket.BaseActivity
import com.android.supermarket.R
import com.android.supermarket.helper.UsbHelper
import java.io.UnsupportedEncodingException

/**
 * Created by pengtaoH on 2018/7/19.
 */
class UsbConnectionActivity : BaseActivity() {


    private var buttonCon: Button? = null
    private var buttonPf: Button? = null
    private var buttonCash: Button? = null
    private var buttonCut: Button? = null
    private var mprintfData: EditText? = null
    private var mprintfLog: EditText? = null
    var mUsbAdmin: UsbHelper? = null




    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        buttonCon = findViewById(R.id.conTest) as Button
        buttonPf = findViewById(R.id.printf) as Button
        buttonCash = findViewById(R.id.buttonCash) as Button
        buttonCut = findViewById(R.id.buttonCut) as Button
        mprintfData = findViewById(R.id.printfData) as EditText
        mprintfLog = findViewById(R.id.printfLog) as EditText
        val buttonListener = ButtonListener()
        buttonCon!!.setOnClickListener(buttonListener)
        buttonPf!!.setOnClickListener(buttonListener)
        buttonCash!!.setOnClickListener(buttonListener)
        buttonCut!!.setOnClickListener(buttonListener)
        mUsbAdmin = UsbHelper(this)
    }


    internal inner class ButtonListener : View.OnClickListener {

        override fun onClick(v: View) {
            when (v.id) {
                R.id.conTest -> if (conTest()) {
                    PrintfLog("USB连接成功...")
                    buttonCon!!.text = "已打开USB"
                    buttonPf!!.isEnabled = true
                    buttonCash!!.isEnabled = true
                    buttonCut!!.isEnabled = true
                } else {
                    PrintfLog("连接失败...")
                    buttonCon!!.text = "打开失败"
                    buttonPf!!.isEnabled = false
                    buttonCash!!.isEnabled = false
                    buttonCut!!.isEnabled = false
                }
                R.id.printf -> try {
                    if (PrintfData((mprintfData!!.text.toString() + "\nExample4USB-XPrinter\n").toByteArray(charset("GBK")))) {
                        PrintfLog("打印成功...")
                    } else {
                        PrintfLog("打印失败...")
                        buttonPf!!.isEnabled = false
                    }
                } catch (e: UnsupportedEncodingException) {
                    e.printStackTrace()
                    PrintfLog("数据发送错误...")
                }

                R.id.buttonCash -> {
                    val SendCash = byteArrayOf(0x1b, 0x70, 0x00, 0x1e, 0xff.toByte(), 0x00)
                    if (PrintfData(SendCash)) {
                        PrintfLog("打开成功...")
                    } else {
                        PrintfLog("打开失败...")
                    }
                }
                R.id.buttonCut -> {
                    val SendCut = byteArrayOf(0x0a, 0x0a, 0x1d, 0x56, 0x01)
                    if (PrintfData(SendCut)) {
                        PrintfLog("切纸成功...")
                    } else {
                        PrintfLog("切纸失败...")
                    }
                }
                else -> {
                }
            }

        }
    }

    fun conTest(): Boolean {
        mUsbAdmin!!.Openusb()
        return mUsbAdmin!!.GetUsbStatus()
    }

    fun PrintfLog(logString: String) {
        mprintfLog!!.setText(logString)
    }

    fun PrintfData(data: ByteArray): Boolean {
        return mUsbAdmin!!.sendCommand(data)
    }
}